'''
Master task

Registers slave tasks.
'''
import traceback
import dexen
import conditions
import  
from individual import Individual
from slave_tasks.dev import DevTask
from slave_tasks.eval import EvalATask, EvalBTask
from slave_tasks.feedback import FeedbackTask

# Master task
class MasterTask(dexen.BaseMasterTask):
    def __init__(self, master_env):
        #register slave tasks
        try:
            assert isinstance(master_env, dexen.MasterEnv)
            master_env.start_task(DevTask, "Dev", settings.DEV_SIZE, conditions.dev_cond)
            master_env.start_task(EvalATask, "EvalA", settings.EVALA_SIZE, conditions.evalA_cond)
            master_env.start_task(EvalBTask, "EvalB", settings.EVALB_SIZE, conditions.evalB_cond)
            master_env.start_task(FeedbackTask, "Feedback", settings.FEED_SIZE, conditions.feedback_cond)
        except:
            traceback.print_tb(sys.exc_info()[2])
        #initialise population
        for _ in range(settings.POP_SIZE):
            ind = Individual()
            master_env.set_ind(ind)
        
    def __execute__(self, master_env):
        assert isinstance(master_env, dexen.MasterEnv)
        pop_size = master_env.num_inds()
        if pop_size >= settings.MAX_BIRTHS:
            master_env.stop_master()
